# coding=utf8
__author__ = 'lijian'

from utils import byteUtil

UNKNOW = 0x00  # 未知
V = 0x01  # 电压
A = 0x02  # 电流
AP = 0x03  # 有功功率
RP = 0x04  # 无功功率
PF = 0x05  # 功率因数
PL = 0x06  # 频率
XJ = 0x0  # 相角
ZD = 0x08  # 照度

_encodeFuncDict = {
    V: byteUtil.encodeA2,
    A: byteUtil.encodeA2,
    AP: byteUtil.encodeA2,
    RP: byteUtil.encodeA2,
    PF: byteUtil.encodeA5,
    PL: byteUtil.encodeA2,
    XJ: byteUtil.encodeA5,
    ZD: byteUtil.encodeA2
}

_decodeFuncDict = {
    V: byteUtil.decodeA2,
    A: byteUtil.decodeA2,
    AP: byteUtil.decodeA2,
    RP: byteUtil.decodeA2,
    PF: byteUtil.decodeA5,
    PL: byteUtil.decodeA2,
    XJ: byteUtil.decodeA5,
    ZD: byteUtil.decodeA2
}


def encodeFunc(type):
    if _encodeFuncDict.has_key(type):
        return _encodeFuncDict[type]
    else:
        return None


def decodeFunc(type):
    if _decodeFuncDict.has_key(type):
        return _decodeFuncDict[type]
    else:
        return None


defaultConfig = {
    "0": {
        "enable": 1,
        "type": V,
        "phase": 1,
        "line": 0|1,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "1": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0|1,
        "onUpperLimit": 300,
        "onLowerLimit": 1.0,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "2": {
        "enable": 1,
        "type": V,
        "phase": 2,
        "line": 0|1,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "3": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0|1,
        "onUpperLimit": 300,
        "onLowerLimit": 1,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "4": {
        "enable": 1,
        "type": V,
        "phase": 3,
        "line": 0|1,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "5": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0|1,
        "onUpperLimit": 100,
        "onLowerLimit": 0.5,
        "offUpperLimit": 0.1,
        "offLowerLimit": 0
    },
    "6": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0|1,
        "onUpperLimit": 3000,
        "onLowerLimit": 1000,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "7": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0|1,
        "onUpperLimit": 3000,
        "onLowerLimit": 1000,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "8": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0|1,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "9": {
        "enable": 1,
        "type": V,
        "phase": 1,
        "line": 0|2,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "10": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0|2,
        "onUpperLimit": 2,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "11": {
        "enable": 1,
        "type": V,
        "phase": 2,
        "line": 0|2,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "12": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0|2,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "13": {
        "enable": 1,
        "type": V,
        "phase": 3,
        "line": 0|2,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "14": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0|2,
        "onUpperLimit": 2,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "15": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0|2,
        "onUpperLimit": 3000,
        "onLowerLimit": 1800,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "16": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0|2,
        "onUpperLimit": 3000,
        "onLowerLimit": 1800,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "17": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0|2,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "18": {
        "enable": 1,
        "type": V,
        "phase": 1,
        "line":0xF|0,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "19": {
        "enable": 1,
        "type": V,
        "phase": 2,
        "line": 0xF|0,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "20": {
        "enable": 1,
        "type": V,
        "phase": 3,
        "line": 0xF|0,
        "onUpperLimit": 300,
        "onLowerLimit": 180,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "21": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0xF|1,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "22": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0xF|1,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "23": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0xF|1,
        "onUpperLimit": 3,
        "onLowerLimit": 0.2,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "24": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0xF|1,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "25": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0xF|1,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "26": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0xF|2,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "27": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0xF|2,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "28": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0xF|2,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "29": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0xF|2,
        "onUpperLimit": 3,
        "onLowerLimit": 0.4,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "30": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0xF|2,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "31": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0xF|2,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "32": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0xF|2,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "33": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0xF|3,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "34": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0xF|3,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "35": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0xF|3,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "36": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0xF|3,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "37": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0xF|3,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "38": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0xF|3,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "39": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0xF|4,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "40": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0xF|4,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "41": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0xF|4,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "42": {
        "enable": 1,
        "type": AP,
        "phase": 129,
        "line": 0xF|4,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "43": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0xF|4,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "44": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0xF|4,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "45": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0xF|5,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "46": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0xF|5,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "47": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0xF|5,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "48": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0xF|5,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "49": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0xF|5,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "50": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0xF|5,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "51": {
        "enable": 1,
        "type": A,
        "phase": 1,
        "line": 0xF|6,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "52": {
        "enable": 1,
        "type": A,
        "phase": 2,
        "line": 0xF|6,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "53": {
        "enable": 1,
        "type": A,
        "phase": 3,
        "line": 0xF|6,
        "onUpperLimit": 3,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "54": {
        "enable": 1,
        "type": AP,
        "phase": 0,
        "line": 0xF|6,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "55": {
        "enable": 1,
        "type": RP,
        "phase": 0,
        "line": 0xF|6,
        "onUpperLimit": 3000,
        "onLowerLimit": 500,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    },
    "56": {
        "enable": 1,
        "type": PF,
        "phase": 0,
        "line": 0xF|6,
        "onUpperLimit": 1,
        "onLowerLimit": 0.5,
        "offUpperLimit": 20,
        "offLowerLimit": 0
    }
}